package org.xhnews.ui;

import java.util.Vector;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.container.MainScreen;

import org.xhnews.bo.NewsBo;
import org.xhnews.to.NewsChannel;

public class ColumnSettingScreen extends MainScreen {
	Vector newschannels,enabledchannels;
	public ColumnSettingScreen() {
		setTitle("选择栏目");
		newschannels=NewsBo.getAllNewsChannels();
		enabledchannels=NewsBo.getAllEnableChannels();
		for(int i=0;i<newschannels.size();i++){
			CheckboxField channel=new CheckboxField();
			channel.setLabel(((NewsChannel)newschannels.elementAt(i)).getTitle());
            if(NewsBo.getIndexOfContainer((NewsChannel)newschannels.elementAt(i),enabledchannels)!=-1)
            	channel.setChecked(true);
            channel.setChangeListener(_channel_listener);
			add(channel);
		}
	}
	private NewsChannel getChannelByTitle(String title,Vector channels){
		for(int i=0;i<channels.size();i++){
			if(((NewsChannel)channels.elementAt(i)).getTitle().equals(title))
				return (NewsChannel)channels.elementAt(i);
		}
		return null;
	}
	FieldChangeListener _channel_listener = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			if(field instanceof CheckboxField){
				String link=getChannelByTitle(((CheckboxField)field).getLabel(),newschannels).getFileName();
                if(((CheckboxField)field).getChecked()){               	
                	NewsBo.setChannelEnable(link, true);
                }else{
                	NewsBo.setChannelEnable(link, false);
                }
			}
		}
	};
}
